package com.jh.web;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.jh.pojo.Entity;
import com.jh.pojo.cmd.PreScanCmdBean;
import com.jh.utils.BlockingQueueUtils;

/**
 * Servlet implementation class GrpPreScanServlet
 */
@WebServlet(description = "Grp PreScan", urlPatterns = { "/GrpPreScanServlet" })
public class GrpPreScanServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(GrpPreScanServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GrpPreScanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String ipArrayStr = request.getParameter("ipArrayJson");
        System.out.println("ipArrayStr: " + ipArrayStr);
        logger.debug("--->" + "ipArrayStr: " + ipArrayStr);

        String startAngle = request.getParameter("startAngle");
        String endAngle = request.getParameter("endAngle");
        String speed = request.getParameter("speed");
        String scanCount = request.getParameter("scanCount");

        logger.debug("--->" + " startAngle: " + startAngle + " endAngle: " + endAngle + " speed: " + speed
                + " scanCount: " + scanCount);

        String[] ipArray = ipArrayStr.split(",");
        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();
        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        Gson gson = new Gson();
        PreScanCmdBean preScanCmdBean = new PreScanCmdBean();
        Entity entity = new Entity();

        for (String ipString : ipArray) {
            preScanCmdBean.setIp(ipString);
            preScanCmdBean.setStartAngle(Integer.parseInt(startAngle));
            preScanCmdBean.setEndAngle(Integer.parseInt(endAngle));
            preScanCmdBean.setSpeed(Integer.parseInt(speed));
            preScanCmdBean.setScanCount(Integer.parseInt(scanCount));

            entity.setType(5);
            entity.setData(preScanCmdBean);
            String json = gson.toJson(entity);
            logger.debug("--->" + " GrpPreScan json: " + json);
            try {
                txBq.put(json);
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);

            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
